/*
 *  How to load jQuery and then use the Search API with it.
 */

google.load("jquery", "1.4.2");
google.load('search', '1');

var clicked = [];
var makeYourOwn = [];
var ctrlPressed = false;
var currentMenuItem = 1;
var cost = 0;

function searchComplete(searcher) {
	// Check that we got results
	if (searcher.results && searcher.results.length > 0) {

		// Loop through our results, printing them to the page.
		var results = searcher.results;
		// For each result write it's title and image to the screen
		var result = results[Math.floor(Math.random()*results.length)];
		$(this).html(
				$(this).html() + "<img style=\"margin-left: 5px; margin-top: 5px\" width=\"140px\" height=\"90px\" src=\""
						+ result.tbUrl + "\" />");
	}
}

function findPic(picName, id) {
	var searcher = new google.search.ImageSearch();
	searcher.setSearchCompleteCallback($("#" + id), searchComplete,
			[ searcher ]);
	searcher.execute(picName);
}

function go_final() {
	currentMenuItem = $(".topic").length;
	$("#finalize").css('color', 'red');
	str = "You customized one smashburger with";
	if(makeYourOwn.length > 0)
		str += " the following:<br />";
	else
		str += "...nothing! *GASP*<br />"
	finalMeal = "";
	finalCost = 0;
	for (i = 0; i < makeYourOwn.length; i++) {
		finalMeal += makeYourOwn[i][1] + "<br />";
	}
	if(makeYourOwn.length > 0)
		finalCost = 4.99 + cost;
	for (i = 0; i < clicked.length; i++) {
		if (i == 0) {
			finalMeal += "<br />And from the menu (ah, you realized we knew what we were doing!): <br />";
		}
		currentItem = $("#" + clicked[i]).children(0);
		finalMeal += currentItem.attr('name') + "<br />";
		finalCost += parseFloat(currentItem.attr('cost'));
	}
	str += finalMeal + "At a final cost of: " + finalCost + "<br />";
	str += "<br />Name your meal! You deserve it!<br />";
	str += "<form id='mainForm' action='/addBurger' method='post'>"
			+ "<input id='burgerName' name='mealName' type='text' />"
			+ "<input id='mealD' type='hidden' name='mealDetails' value='"
			+ finalMeal + "' />"
			+ "<input id='mealC' type='hidden' name='mealCost' value='"
			+ finalCost + "' />" + "<input type='submit' /></form>";
	$("#topic-final").html(str);
	$("#topic-final").show();
}

function Load() {

	$("#topic-link-" + currentMenuItem).css('color', 'red');

	$('.food').each(function(index) {
		$(this).toggle(function() {
			current = $(this);
			id = current.attr('id');
			clicked.push(id);
			$("#" + id).css('background', 'gray');
		}, function() {
			current = $(this);
			id = current.attr('id');
			clicked.splice(clicked.indexOf(id), 1);
			$("#" + id).css('background', 'white');
		});
	});

	$('.food').bind(
			'mouseover',
			function(event) {
				current = $(event.target).parent();
				i = current.html().indexOf("name=\"", 0) + 6;
				j = current.html().indexOf("\"", i);
				k = current.html().substring(i, j);
				if (!current.html().match("bubble"))
					current.html(current.html() + "<div class='bubble'>" + k
							+ "</div>");
			});

	$('.food').bind('mouseout', function(event) {
		current = $(event.target).parent();
		i = current.html().indexOf("<div class=\"bubble\"", 0);
		if (i > 0)
			current.html(current.html().substring(0, i));
	});

	$('#orderLink').toggle(function() {
		$("#orderContent").show("slow")
	}, function() {
		$("#orderContent").hide("slow")
	});

	$('#previousLink').toggle(function() {
		$("#previousContent").show("slow")
		return false;
	}, function() {
		$("#previousContent").hide("slow")
		return false;
	});

	$('#makeYourOwnLink').toggle(function() {
		$("#makeYourOwnContent").hide("slow")
		return false;
	}, function() {
		$("#makeYourOwnContent").show("slow")
		return false;
	});

	$('#prevMakeYourOwn').bind('click', function() {
		if (currentMenuItem != 1) {
			$("#finalize").css('color', 'blue');
			$("#topic-final").hide()
			len = $(".topic").length;
			$("#topic-" + currentMenuItem).hide();
			$("#topic-link-" + currentMenuItem).css('color', 'blue');
			currentMenuItem = (currentMenuItem + len - 2) % len + 1;
			$("#topic-" + currentMenuItem).show();
			$("#topic-link-" + currentMenuItem).css('color', 'red');
		}
		return false;
	});

	$('#nextMakeYourOwn').bind('click', function() {
		len = $(".topic").length;
		$("#topic-" + currentMenuItem).hide();
		$("#topic-link-" + currentMenuItem).css('color', 'blue');
		if (currentMenuItem < len) {
			currentMenuItem = currentMenuItem % len + 1;
			$("#topic-" + currentMenuItem).show();
			$("#topic-link-" + currentMenuItem).css('color', 'red');
		} else {
			go_final();
		}
		return false;
	});

	$('.topicLink').each(function(index) {
		$(this).html($(this).html().toLowerCase());
		$(this).bind('click', function() {
			$("#topic-" + currentMenuItem).hide();
			$("#topic-link-" + currentMenuItem).css('color', 'blue');
			$("#finalize").css('color', 'blue');
			$("#topic-final").hide()
			if ($(this).attr('id')=="finalize") {
				go_final();
			} else {
				str = "topic-link-";
				k = $(this).attr('id').indexOf(str) + str.length;
				num = parseInt($(this).attr('id').substring(k));
				$("#topic-" + currentMenuItem).hide();
				$("#topic-link-" + currentMenuItem).css('color', 'blue');
				currentMenuItem = num;
				$("#topic-link-" + currentMenuItem).css('color', 'red');
				$("#topic-" + currentMenuItem).show();
			}
		});
	});

	$('.menu_item').each(
			function(index) {
				$(this).html(
						findPic($(this).html().substring(0,
								$(this).html().indexOf("|")), $(this)
								.attr('id')));
				$(this).bind(
						'click',
						function() {
							index = $(this).html().indexOf("|");
							str = $(this).html().substring(0, index);
							price = $(this).html().substring(index + 3,
									index + 7);
							k = makeYourOwn.length;
							for (i = 0; i < k; i++) {
								if (makeYourOwn[i][0] == currentMenuItem
										&& makeYourOwn[i][1] == str) {
									makeYourOwn.splice(i, 1);
									$(this).css('background', '#fff6bf');
									cost -= parseFloat(price);
									$("#whatWeHave").html(cost);
									return false;
								}
							}
							cost += parseFloat(price);
							makeYourOwn.push( [ currentMenuItem, str ]);
							$("#whatWeHave").html(makeYourOwn.toString());
							$(this).css('background', '#ffd324');
						});
			});

	$(window).keydown(function(evt) {
		if (evt.which == 17) { // ctrl
				ctrlPressed = true;
			}
		}).keyup(function(evt) {
		if (evt.which == 17) { // ctrl
				ctrlPressed = false;
			}
		});

}
google.setOnLoadCallback(Load);
